/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"stringiter.hpp"

#include	<iostream>



/**
	Constant value.
	
*/
const string	StringIterator::defaultSeparator			= ",";


/**
	Constructor.

*/
StringIterator::StringIterator(const string& a_strOriginal )
{
	parse(a_strOriginal, defaultSeparator );
}


/**
	Constructor.

*/
StringIterator::StringIterator(const string& a_strOriginal, const string& a_strSeparator )
{
	parse(a_strOriginal, a_strSeparator );
}
		
		
/**
	Gives the current element in the string.
	
*/
const string	StringIterator::value(void ) const
{
	return	m_vectorChunks[m_iPosition ];
}


/**
	Gives the number of elements in the string.
	
*/
int	StringIterator::size(void ) const
{
	return	m_vectorChunks.size();
}
		
		
/**
	Gives the current position.
	
*/
int	StringIterator::getPosition(void ) const
{
	return	m_iPosition;
}


/**
	Moves to first element in the string.
	
*/
bool	StringIterator::begin(void )
{
	m_iPosition				= 0;
	
	return	m_iPosition < size();
}


/**
	Moves to last element in the string.
	
*/
bool	StringIterator::end(void )
{
	m_iPosition				= size() - 1;
	
	return	(m_iPosition >= 0 );
}


/**
	Moves to the next element in the string.
	
*/
bool	StringIterator::next(void )
{
	return	(++m_iPosition < size() );
}


/**
	Moves to the previous element in the string.
	
*/
bool	StringIterator::prev(void )
{
	return	(--m_iPosition >= 0 );
}


/**
	Gives the element at the given index.
	
*/
string	StringIterator::operator[](int a_iIndex ) const
{
	return	m_vectorChunks[a_iIndex ];
}
		
		
/**
	Moves to the next element in the string.
	
*/
bool	StringIterator::operator++(void )
{
	return	next();
}


/**
	Moves to the previous element in the string.
	
*/
bool	StringIterator::operator--(void )
{
	return	prev();
}


/**
	Convertor to string.
	
*/
StringIterator::operator string(void )
{
	return	value();
}


/**
	Splits a string into its component parts.

*/
void	StringIterator::parse(const string& a_strOriginal, const string& a_strSeparator )
{
	int					iSizeSeparator			= a_strSeparator.size();
	string::size_type	iStartPos				= 0;
	string::size_type	iEndPos;
	
	//MW:	Loop to find all (if any) separators...
	do
	{
		string				strChunk;

		iEndPos				= a_strOriginal.find(a_strSeparator, iStartPos );
		if (iEndPos == string::npos )
		{
			strChunk			= a_strOriginal.substr(iStartPos );
		}
		else
		{
			strChunk			= a_strOriginal.substr(iStartPos, iEndPos - iStartPos );
			iStartPos			= iEndPos + iSizeSeparator;
		}

		if (strChunk.size() > 0 )
		{
			//MW:	Store this chunk...
			m_vectorChunks.push_back(Util::trim(strChunk ) );
		}
	}
	while (iEndPos != string::npos );
	
	//MW:	Initialise position...
	m_iPosition			= 0;
}
